package com.link.base.base.cfgproperty.service;


import com.link.base.user.model.CoreUser;
import com.link.base.base.cfgproperty.dao.mybatis.mapper.CfgPropertyMapper;
import com.link.base.base.cfgproperty.model.CfgProperty;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.*;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title</p>
 * <p>Description</p>
 * <p>Company</p>
 *
 * @author yrf
 * @date 2016年8月4日  下午4:14:22
 */
@Service
public class CfgPropertyServiceImpl extends BasicServiceImpl<CfgProperty> implements CfgPropertyService {

    @Resource
    private CfgPropertyMapper cfgPropertyMapper;

    @Override
    public BasicMapper<CfgProperty> getBasicMapper() throws Exception {
        if (cfgPropertyMapper != null) {
            return cfgPropertyMapper;
        }
        throw new ServiceException("PUBLIC-006");
    }


    @Override
    public void loadCfg(CfgProperty cfgProperty) throws Exception {
        if (cfgProperty == null) {
            cfgProperty = new CfgProperty();
        }
        cfgProperty.setPageFlag(false);
        List<CfgProperty> list = cfgPropertyMapper.queryAll(cfgProperty);


        loadCfgToRedis(list);
    }

    private void loadCfgToRedis(List<CfgProperty> list) {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            Map<String, String> comMap = new HashMap<String, String>(16);
            Map<String, Map<String, String>> priMapMap = new HashMap<String, Map<String, String>>(16);
            for (CfgProperty cfgProperty : list) {
                if (AppConstants.ADMIN_CORP_ID.equals(cfgProperty.getCorpid()) && "Y".equals(cfgProperty.getType())) {
                    //管理账套并且为公共参数的数据设置到公共参数里
                    comMap.put(cfgProperty.getKey(), cfgProperty.getValue());

                } else {
                    //其余都是账套私有参数
                    String redisKey = RedisConstants.SYS_PROPERTY_CORP_KEY + cfgProperty.getCorpid();
                    Map<String, String> priMap = priMapMap.get(redisKey);
                    if (priMap == null) {
                        priMap = new HashMap<String, String>(16);
                        priMapMap.put(redisKey, priMap);
                    }
                    priMap.put(cfgProperty.getKey(), cfgProperty.getValue());
                }
            }
            if (comMap.size() > 0) {
                //删除原公共参数
                jedis.del(RedisConstants.SYS_PROPERTY_COMMON_KEY);
                //设置新公共参数
                jedis.hmset(RedisConstants.SYS_PROPERTY_COMMON_KEY, comMap);
            }
            if (priMapMap.size() > 0) {
                //遍历私有参数
                for (Map.Entry<String, Map<String, String>> priMapEntry : priMapMap.entrySet()) {
                    //删除原私有参数
                    jedis.del(priMapEntry.getKey());
                    //设置新私有参数
                    jedis.hmset(priMapEntry.getKey(), priMapEntry.getValue());
                }
            }
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }


    @Override
    public void beforUpsert(CfgProperty cfgProperty) {
        String flag = "Y";
        if (flag.equals(cfgProperty.getType())) {
            CoreUser user = UserUtil.getUser();
            if (!AppConstants.ADMIN_CORP_ID.equals(user.getCorpid())) {
                throw new ServiceException("CFGPROPERTY-001");
            }
        }
        if (StringUtils.isBlank(cfgProperty.getType())) {
            //是否公共参数
            cfgProperty.setType("N");
        }
    }

    @Override
    public void beforDelete(CfgProperty cfgProperty) throws Exception {

    }
}
